package com.study.aidlsample;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.study.aidlsample.model.Message;
import com.study.aidlsample.service.RemoteService;

public class MainActivity extends AppCompatActivity {

    TextView tv_hello;
    private IMessageSender iMessageSender;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull android.os.Message msg) {
            tv_hello.setText(msg.obj.toString());
            return false;
        }
    });
    /**
     * 定义用于接收消息回调的实现类
     */
    private IMessageReceiver iMessageReceiver = new IMessageReceiver.Stub() {
        @Override
        public void onMessageReceived(final Message msg) throws RemoteException {
            //因为消息是从远程服务的线程过来的,所以需要调度到主线程执行
            handler.sendMessage(handler.obtainMessage(100, msg.getContent()));
        }
    };
    /**
     * 绑定服务打开连接的实例
     */
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //获取到服务给我们返回的Binder对象,并转换成IMessageSender对象
            iMessageSender = IMessageSender.Stub.asInterface(service);

            Message message = new Message();
            message.setContent("这是消息的内容");
            message.setContent("消息来自:" + MainActivity.class.getName());
            message.setTo("消息要发送给:" + RemoteService.class.getName());

            try {
                iMessageSender.sendMessage(message);
                iMessageSender.registerReceiveListener(iMessageReceiver);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(MainActivity.class.getName(), "服务连接断开");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_hello = findViewById(R.id.tv_hello);

        OnClick onClick = new OnClick();
        tv_hello.setOnClickListener(onClick);
    }

    class OnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            //绑定服务进行通信
            Intent intent = new Intent(MainActivity.this, RemoteService.class);
            bindService(intent, connection, BIND_AUTO_CREATE);
            startService(intent);
        }
    }


    @Override
    protected void onDestroy() {
        //解绑服务
        if (iMessageSender != null && iMessageSender.asBinder().isBinderAlive()) {
            try {
                iMessageSender.unregisterReceiveListener(iMessageReceiver);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        unbindService(connection);
        super.onDestroy();
    }
}